package com.hsd.netty.client.strategy;

import com.hsd.netty.client.common.NettyHeader;
import com.hsd.netty.client.common.NettyMessage;
import com.hsd.netty.client.enums.MessageCode;
import com.hsd.netty.client.cache.OverallStaticCache;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author s.he
 * on 2020/6/6
 */
public class HeartBeatStrategy extends NettyMessageAdapter {
    private static final Logger logger = LoggerFactory.getLogger(HeartBeatStrategy.class);

    @Override
    public void handleServerResponse(ChannelHandlerContext ctx, Object msg) {
        NettyMessage nettyMessage = (NettyMessage)msg;
        NettyHeader nettyHeader = nettyMessage.getNettyHeader();
        if(nettyHeader.getStatusCode().equals(MessageCode.SUCCESS.getCode())) {
            logger.info("【心跳响应】接受到服务器端返回的心跳信息");
            OverallStaticCache.setSessionId(String.valueOf(nettyHeader.getSessionId()));
        } else {
            logger.info("和服务器心跳连接失败，返回的错误代码为:" + nettyHeader.getStatusCode());
        }

    }
}
